<?php

class ShopConfiguration extends Configuration {
	protected $AppName = "shop";
	protected $IncludeFiles = array("shop.category.class.php", "shop.item.class.php");
	public $UseApps = array(array("name" => "mysql"), array("name" => "smarty"), array("name" => "shop", "method" => "getShop"));
	
	function registerMethods(AdminController $admin){
		$admin->register($this->AppName, "add-category", "addCategoryInterface", $this->Language->get("add_category"));			// DONE
		$admin->register($this->AppName, "list-categories", "listCategoriesInterface", $this->Language->get("list_categories"));// DONE
		$admin->register($this->AppName, "add-item", "addItemInterface", $this->Language->get("add_item"));						// DONE	
		$admin->register($this->AppName, "list-orders", "listOrdersInterface", $this->Language->get("list_orders"));											// DONE	
				
		$admin->register($this->AppName, "edit-category", "editCategoryInterface", null);										// DONE
		$admin->register($this->AppName, "delete-category", "deleteCategoryInterface", null);									// DONE
		$admin->register($this->AppName, "list-items", "listItemsInterface", null);												// DONE
		$admin->register($this->AppName, "edit-item", "editItemInterface", null);												// DONE											
		$admin->register($this->AppName, "delete-item", "deleteItemInterface", null);											// DONE
	}
	
	function addCategoryInterface(){
		if(isset($_POST["addCategory"])){
			$MySQL = $this->UseApps["mysql"];
			
			$CategoryName = escape($_POST["category_name"], $MySQL);
			
			$MySQL->query("INSERT INTO {$MySQL->getPrefix()}shop_categories (category_name) VALUES ('$CategoryName')");
			
			return $this->Language->get("category_added");
		}
		
		$this->ViewFile = "config_add_category.tpl";
		return true;
	}
	
	function listCategoriesInterface(){
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		$ShopC = $this->UseApps["shop"];
		
		$Categories = $ShopC->getCategories();
		
		$Smarty->assign("AllCategories", $Categories);
		
		$this->ViewFile = "config_list_categories.tpl";
		
		return true;
	}
	
	function editCategoryInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		
		$CatID = $HTTP->get($URLPosition+3);
		
		if(isset($_POST["editCategory"])){
			$MySQL = $this->UseApps["mysql"];
			$NewCategoryName = escape($_POST["category_name"], $MySQL);
			
			$MySQL->query("UPDATE {$MySQL->getPrefix()}shop_categories SET category_name = '{$NewCategoryName}' WHERE id = $CatID");
			
			return $this->Language->get("category_updated");
		}
		
		if(is_numeric($CatID)){
			$Category = new ShopCategory($CatID, $this);
		}
		
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		$Smarty->assign("ShopCategory", $Category);
		$this->ViewFile = "config_edit_category.tpl";
		
		return true;
	}
	
	function deleteCategoryInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		
		$CatID = $HTTP->get($URLPosition + 3);
		
		if(is_numeric($CatID)){
			$MySQL = $this->UseApps["mysql"];
			$MySQL->query("DELETE FROM {$MySQL->getPrefix()}shop_categories WHERE id = $CatID");
			
			$MySQL->query("DELETE FROM {$MySQL->getPrefix()}shop_items WHERE category_id = $CatID");
			
			return $this->Language->get("category_deleted");	
		}
	}
	
	function addItemInterface(){
		if(isset($_POST["addItem"])){
			$MySQL = $this->UseApps["mysql"];
			
			$ItemName = escape($_POST["item_name"], $MySQL);
			$ItemPrice = floatval($_POST["item_price"]);
			$ItemDiscount = floatval($_POST["item_discount"]);
			$ItemCode = escape($_POST["item_code"], $MySQL);
			$CategoryID = intval($_POST["category_id"]);
			$ItemAvalible = ($_POST["item_avalible"] == "yes") ? "true" : "false";
			$ItemSDescription = escape($_POST["item_short_description"], $MySQL);
			$ItemLDescription = escape($_POST["item_long_description"], $MySQL);
			$ItemImage = $this->uploadImage($_FILES["item_image"]);
			
			if($ItemImage === false){
				return "Error uploading picture.";
			}
			
			$MySQL->query("INSERT INTO {$MySQL->getPrefix()}shop_items (item_name, item_price, item_discount, item_code, category_id, item_avalible, item_short_description, item_long_description, item_image) VALUES('$ItemName', $ItemPrice, $ItemDiscount, '$ItemCode', $CategoryID, '$ItemAvalible', '$ItemSDescription', '$ItemLDescription', '$ItemImage')");
			
			return $this->Language->get("item_added");
		}
		
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		$Shop = $this->UseApps["shop"];
		
		$AllCategories = $Shop->getCategories();
		
		$Smarty->assign("AllCategories", $AllCategories);
		$this->ViewFile = "config_add_item.tpl";
		
		return true;
	}
	
	function listItemsInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		
		$CatID = $HTTP->get($URLPosition+3);
		
		if(!is_numeric($CatID)){
			return $this->Language->get("invalid_category_id");
		}
		
		$Category = new ShopCategory($CatID, $this);
		
		if($Category->getID() == null){
			return $this->Language->get("invalid_category_id");
		}
		
		if(count($Category->getItems()) <= 0){
			return $this->Language->get("category_empty");
		}
		
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		$Smarty->assign("Category",$Category);
		$this->ViewFile = "config_list_items.tpl";
		
		return true;
	}
	
	function editItemInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		
		$ItemID = $HTTP->get($URLPosition + 3);
		
		if(!is_numeric($ItemID)){
			return $this->Language->get("invalid_item_id");
		}
		
		$Item = new ShopItem($ItemID, $this);
		
		if($Item->getID() == null){
			return $this->Language->get("invalid_item_id");
		}
		
		if(isset($_POST["editItem"])){
			$MySQL = $this->UseApps["mysql"];
			
			$ItemName = escape($_POST["item_name"], $MySQL);
			$ItemPrice = floatval($_POST["item_price"]);
			$ItemDiscount = floatval($_POST["item_discount"]);
			$ItemCode = escape($_POST["item_code"], $MySQL);
			$CategoryID = intval($_POST["category_id"]);
			$ItemAvalible = ($_POST["item_avalible"] == "yes") ? "true" : "false";
			$ItemSDescription = escape($_POST["item_short_description"], $MySQL);
			$ItemLDescription = escape($_POST["item_long_description"], $MySQL);
			$ItemImage = ($_FILES["item_image"]["name"] != "") ? $this->uploadImage($_FILES["item_image"]) : false;
				
			$SQL = "SET item_name = '$ItemName', item_price = $ItemPrice, item_discount = $ItemDiscount, item_code = '$ItemCode', category_id = $CategoryID, item_avalible = '$ItemAvalible', item_short_description = '$ItemSDescription', item_long_description = '$ItemLDescription'";
			
			if($ItemImage != false){
				$SQL .= ", item_image = '$ItemImage'";
			}
			
			$MySQL->query("UPDATE {$MySQL->getPrefix()}shop_items ".$SQL." WHERE id = $ItemID");
			
			return $this->Language->get("item_edited");
		}
		
		
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		$Shop = $this->UseApps["shop"];
		
		$Categories = $Shop->getCategories();
		
		$Smarty->assign("Item", $Item);
		$Smarty->assign("AllCategories", $Categories);
		
		$this->ViewFile = "config_edit_item.tpl";
		
		return true;
	}
	
	function deleteItemInterface(){
		$URLPosition = SystemRegistry::getInstance()->get("url_position");
		$HTTP = new HTTPRequest();
		$ItemID = $HTTP->get($URLPosition+3);
		
		if(is_numeric($ItemID)){
			$MySQL = $this->UseApps["mysql"];
			$MySQL->query("DELETE FROM {$MySQL->getPrefix()}shop_items WHERE id = $ItemID");
			
			return $this->Language->get("item_deleted");
		}
		
		return $this->Language->get("invalid_item_id");
	}
	
	function listOrdersInterface(){
		$MySQL = $this->UseApps["mysql"];
		
		$SelectOrders = $MySQL->query("SELECT * FROM {$MySQL->getPrefix()}shop_orders ORDER BY order_time DESC");
		
		$Orders = array();
		
		while ($order = mysql_fetch_assoc($SelectOrders)) {
			$Orders[] = $order;
		}
		
		$SmartyC = $this->UseApps["smarty"];
		$Smarty = $SmartyC->getSmarty();
		
		$Smarty->assign("Orders", $Orders);
		$this->ViewFile = "config_list_orders.tpl";
		
		return true;
	}
	
	function uploadImage($file_information){
		$DirPath = dirname(__FILE__)."/../../content/shop/";
		$NewFileName = preg_replace("#[^a-z0-9\.]#", "", strtolower($file_information["name"]));
		
		if(move_uploaded_file($file_information["tmp_name"], $DirPath.$NewFileName)){
			return $NewFileName;
		}
		
		return false;
	}
	
}

?>